namespace Microsoft.ServiceModel.Samples.Discovery
{
    using System;
    using System.Xml;
    using System.Threading;
    using System.Diagnostics;
    using System.Collections.Generic;
    
    internal abstract class FindContext
    {
        UniqueId findId;
        TimeSpan findDuration;
        int maxResults;
        Timer timer;
        List<ServiceProperties> results;
        FindAsyncResult asyncResult;

        public FindContext(TimeSpan findDuration, int maxResults)
        {
            this.findId = new UniqueId();
            this.findDuration = findDuration;
            this.maxResults = maxResults;
            this.results = new List<ServiceProperties>();
        }

        public UniqueId FindId
        {
            get
            {
                return this.findId;
            }
        }

        public int MaxResults
        {
            get
            {
                return this.maxResults;
            }
        }

        public TimeSpan FindDuration
        {
            get
            {
                return this.findDuration;
            }
        }

        public List<ServiceProperties> Results
        {
            get
            {
                return this.results;
            }
        }

        public FindAsyncResult AsyncResult
        {
            get
            {
                return this.asyncResult;
            }

            set
            {
                this.asyncResult = value;
            }
        }

        public void StartTimer(TimerCallback callback)
        {
            lock (this)
            {
                Debug.Assert(this.timer == null);
                this.timer = new Timer(callback, this, FindDuration, 
                    TimeSpan.Zero);
            }
        }

        public void StopTimer()
        {
            lock (this)
            {
                if (this.timer != null)
                {
                    this.timer.Dispose();
                    this.timer = null;
                }
            }
        }
    }
}
